Carbon

     

Working With Stacks

The main thread, which is created by the Process Manager when it launches an application, is the only thread whose stack resides in the application stack area--the stacks for threads that you create reside in the application heap area. The main thread's stack in a threaded application is identical to the stack in a nonthreaded application. Therefore, to increase the size of the main thread's stack in a threaded application, you can use the same Memory Manager commands that you would use in a nonthreaded application. Listing 6 shows how to do this.

Listing 6  Increasing the size of the main thread's stack area

OSErr IncreaseApplicationStack(Size incrementSize)
{
OSErr retCode;
    SetApplLimit((Ptr) ((unsigned long) GetApplLimit()
        incremmentSize));
retCode=MemError();
if(retCode==noErr)
    MaxApplZone();

return retCode;
}

IMPORTANT

You call the function in Listing 6 only once at the beginning of your application. You must call it before any other threads in the application allocate memory. To be safe you should call it before any other threads run, because running another thread could trigger a call to the LoadSeg function (on a 680x0 machine only), which allocates memory and could grow the heap.

For threads that you create in your application, the Thread Manager maintains a separate stack in the application heap area. You specify the stack size when you create a new thread with the CreateThreadPool or NewThread function. The stack must be large enough to handle saved thread context, normal application stack usage, interrupt handling routines, and CPU exceptions. You can specify a particular size in bytes or use the default size that the Thread Manager supplies for a thread. The default size, in most cases, is more than adequate for your needs.

You can call GetDefaultThreadStackSize to determine the default amount of space that the system allocates for threads.

If during testing you find that the stack size is inadequate for an individual thread, you can increase the amount of space for it when you create the thread with the CreateThreadPool or NewThread function. Listing 7 shows how to determine the current stack space for a particular thread and how to increase it.

Listing 7  Determining and increasing the stack size of a thread

OSErr IncreaseThreadStack(ThreadID testThread)
{
    anError = ThreadCurrentStackSpace(testThread, currentStackSize);
    anError = DisposeThread(testThread, 0, 0)
    anError = NewThread(kCooperativeThread, 
                             DoSomething, 
                             nil,
                             (currentStackSize) + 1000,
                             kNoCreationOptions, 
                             nil,
                             testThread);
}

The ThreadCurrentStackSpace function returns, in the currentStackSize parameter, the amount of space available to the thread named testThread . Since you have already determined that this size is inadequate, you dispose of the thread by calling DisposeThread . Then NewThread creates a new thread. The third parameter specifies the stack space to allocate for this thread. In this case, the original amount is increased by a thousand bytes.


© 2000 Apple Computer, Inc. – (Last Updated 09 May 00)